import request from "@/utils/request";

//定义类型
type Common<T> = {
    code: string;
    data: T
    message: string;
    time:string
}
export type MenuItem = {
    createdBy: string;
    createdTime: string;
    description: string;
    href: string;
    icon: string;
    id:number;
    level: number;
    name: string;
    operatorId: number | null;
    orderNum: number;
    parentId: number ;
    shown:boolean;
    updatedBy: string;
    updatedTime: string;
};

//获取所有菜单
export const getAll = () =>{
   return request<Common<MenuItem[]>>({
    method: "get",
    url: "/boss/menu/getAll",
})
}

export type CreateOrEditMenu = Pick<MenuItem, "id" | "parentId" | "name" | "icon" | "href" | "description" | "orderNum" | "shown"> & { id?: number };
// pick表示从MenuItem中挑选出指定的属性  Omit表示从MenuItem中排除指定的属性 

//创建或编辑菜单
export const saveOrUpdate = (menuInfo: CreateOrEditMenu) =>{
    return request<Common<boolean>>({
        method: "post",
        url: "/boss/menu/saveOrUpdate",
        //请求类型是application/json 直接用data传递数据
        data: menuInfo
    })
}

//删除菜单
export const deleteMenu = (id:string) =>{
    return request<Common<boolean>>({
        method: "delete",
        url: `/boss/menu/${id}`

})
}
type SubMenuList = MenuItem & { subMenuList: SubMenuList[] | null}
type EdidMenuInfo = Common<{
    menuInfo : MenuItem
    parentMenuList: SubMenuList[]
}>

//获取制定ID的菜单信息
export const getEdidMenuInfo = (id:string) =>{
     return request<EdidMenuInfo>({
        method: "get",
        url: "/boss/menu/getEditMenuInfo",
        params: {
            id,
        }
     })

}